/*
 * Created on Sep 10, 2004
 */
package com.cb.eclipse.folding.java.calculation;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.JavaModelException;

import com.cb.eclipse.folding.FoldingPlugin;
import com.cb.eclipse.folding.java.preferences.PreferenceKeys;

/**
 * TODO Javadoc.
 * @author R.J. Lorimer
 */
public class UserDefinedRegionHelper {
	private static final ILog L = FoldingPlugin.getDefault().getLog();
	
	private String openingSentinelPrefix;
	private String closingSentinelPrefix;
	
	private String openingSentinelSuffix;
	private String closingSentinelSuffix;
	
	public UserDefinedRegionHelper() {
		
		//computeSentinelBoundries();
	}
	
	
	public boolean isOpeningSentinel(int start, int end, IJavaElement owner) throws JavaModelException {
		String startSentinel = FoldingPlugin.getPrefs().getString(PreferenceKeys.USER_DEFINED_START);
		return isSentinel(startSentinel, start, end, owner);
	}
	
	public boolean isClosingSentinel(int start, int end, IJavaElement owner) throws JavaModelException {
		String endSentinel = FoldingPlugin.getPrefs().getString(PreferenceKeys.USER_DEFINED_END);
		return isSentinel(endSentinel, start, end, owner);
	}
	
	public boolean isSentinel(String sentinel, int start, int end, IJavaElement owner) throws JavaModelException {
		ISourceReference reference = (ISourceReference) owner;
		
		String contents = reference.getSource();
		ISourceRange range = (ISourceRange)reference.getSourceRange();
		
		
		int correctedStart = start - range.getOffset();
		// Math.min should be redundant (no JavaElement should contain half of a comment)
		int correctedEnd = Math.min( (end - range.getOffset()) , range.getLength());
		try {			
			//System.out.println("Checking for sentinel: " + contents.substring(correctedStart, correctedEnd));
			boolean keepScanning = true;
			int shift = correctedStart+2; // +2 refers to the // characters which we know we can remove
			
			while(keepScanning && shift < correctedEnd) {
				char nextChar = contents.charAt(shift);
				
				if(nextChar != ' ' && nextChar != '\t') {
					keepScanning = false;				
				}
				else {
					shift++;
				}
				
			}
			
			boolean foundSentinel = (contents.startsWith(sentinel, shift));
			//System.out.println("Sentinel was found: " + foundSentinel);
			return foundSentinel;
		} catch(StringIndexOutOfBoundsException siobe) {
			L.log(new Status(Status.ERROR, "cb.eclipse.folding", 8888, "isSentinel[start="
					+start+";end="+end+";range.offset="+range.getOffset()
					+";range.length="+range.getLength()
					+";correctedStart="+correctedStart
					+";correctedEnd="+correctedEnd
					+";SRC="+contents+"]"
					, siobe));
		}
		
		return false;
	}
	
	public String getNameForSentinel(String sentinelBody) {
		return null;
	}
	
	private void computeSentinelBoundries() {
		
		String userStart = FoldingPlugin.getPrefs().getString(PreferenceKeys.USER_DEFINED_START);
		String userEnd = FoldingPlugin.getPrefs().getString(PreferenceKeys.USER_DEFINED_END);
		
		int sentinelIdx = userStart.indexOf("%n");
		openingSentinelPrefix = userStart.substring(0, sentinelIdx).trim();
		sentinelIdx+=2;
		if(sentinelIdx < userStart.length()) {
			openingSentinelSuffix = userStart.substring(sentinelIdx+2, userStart.length()).trim();
		}
		
			
		sentinelIdx = userEnd.indexOf("%n");
		closingSentinelPrefix = userEnd.substring(0, sentinelIdx).trim();
		sentinelIdx+=2;
		if(sentinelIdx < userEnd.length()) {
			closingSentinelSuffix = userEnd.substring(sentinelIdx+2, userEnd.length()).trim();
		}
		
	}
	
}
